/*  
 * cafe-legentpicker.js
 * @Author :      GUORUI
 * @CreateDate :  2017年6月20日
 * Copyright (c) 2017, NRIET.　
 * 日期控件
 */

;define(function(){
	/**
	 * 创建控件界面
	 */
	function getNodeHtml(cfg){
		var wrapper = $('<div class="legentpicker">'),
			reset = $('<div id="legentpicker-lengent-reset">'),
			slider = $('<div id="legentpicker-slider">'),
			unit = $('<div id="legentpicker-lengent-unit">');
		wrapper.css({
			width : cfg.totalWidth+"px"
		});
		if(cfg.isShowSlider){
			reset.append($('<i>').addClass("cafe-ifont cafe-ic-chexiao"));
			slider.append(getLegendSlider(cfg));
			wrapper.append(reset).append(slider);
		}
		wrapper.append(getLegendColor(cfg)).append(getLegendNum(cfg));
		if(cfg.unit && "" != cfg.unit){
			unit.text('单位：'+cfg.unit);
			wrapper.append(unit);
		}
		return wrapper;
	}
	// 图例滑动事件
	function getLegendSlider(cfg) {
		var sliderhtml = $('<div class="legentpicker-slider-range">'),
			topHandle = $('<div class="legentpicker-slider-handle-top">'),
			bottomHandle = $('<div class="legentpicker-slider-handle-bottom">');
		topHandle.css("left","-4px");
		bottomHandle.css("left",(cfg.totalWidth-4)+"px");
		sliderhtml.css({
			width : cfg.totalWidth+"px"
		});
		sliderhtml.append(topHandle).append(bottomHandle);
		return sliderhtml;
	}
	// 生成图例颜色
	function getLegendColor(cfg) {
		var colorhtml =  $('<div id="legentpicker-lengent-color">');
		var colArray = cfg.imageData;
		var borSty = cfg.isShowSlider?"none":"none";
		var width = cfg.totalWidth / (Math.round(colArray.length / 4));
		switch (cfg.seq) {
		case "decrease":// 递减
			for (var i = 0, len = colArray.length / 4; i < len; i++) {
				var legend_block = $('<div class="legentpicker-legend-block">');
				var color = "rgba("+colArray[4 * i]+ ','+ colArray[4 * i + 1]+ ','+ colArray[4 * i + 2]+ ',255'+")";
				legend_block.css({
					"border" : borSty,
					"width" : width,
					"height" : cfg.h_legend+"px",
					"float": 'left',
					"background-color" : color
				});
				colorhtml.append(legend_block);
			}
			break;
		case "increase":// 递增
			for (var i = colArray.length / 4; i >= 1; i--) {
				var legend_block = $('<div class="legentpicker-legend-block">');
				var color = "rgba("+colArray[4 * i - 4]+ ','+ colArray[4 * i - 3]+ ','+ colArray[4 * i - 2]+ ',255'+")";
				legend_block.css({
					"border" : borSty,
					"width" : width,
					"height" : cfg.h_legend+"px",
					"float": 'left',
					"background-color" : color
				});
				colorhtml.append(legend_block);
			}
			break;
		default:
			break;
		}
		return colorhtml;
	}
	// 生成图例数字
	function getLegendNum(cfg) {
		var numhtml =  $("<div id='legentpicker-lengent-num'>");
		var numArray = cfg.numData;
		var numWidth = cfg.totalWidth / (numArray.length+1);
		switch (cfg.seq) {
		case "decrease":// 递减
			for (var j = 0, len = numArray.length; j < len; j++) {
				var legend_block = $('<div class="legentpicker-legend-block">');
				legend_block.css({width:numWidth+"px",height:cfg.h_legend+"px",float:"left"}).text(numArray[j]);
				numhtml.append(legend_block);
			}
			numhtml.css({top:"15px",left:numWidth});
			break;
		case "increase":// 递增
			for (var j = numArray.length ; j >= 0; j--) {
				var legend_block = $('<div class="legentpicker-legend-block">');
				legend_block.css({width:numWidth+"px",height:cfg.h_legend+"px",float:"left"}).text(numArray[j]);
				numhtml.append(legend_block);
			}
			numhtml.css({top:"15px",left:numWidth});
			break;
		default:
			break;
		}
		return numhtml;
	}
	/**
	 * 绑定事件
	 */
	function _eves($drop,cfg){
		var step = parseFloat($drop.find(".legentpicker-legend-block").css("width"));
		$drop.undelegate().delegate("#legentpicker-lengent-reset","click",function(){//重置色例
			$drop.find(".legentpicker-slider-handle-top").css("left","-4px");
			$drop.find(".legentpicker-slider-handle-bottom").css("left",(cfg.totalWidth-4)+"px");
			changeGisRange($drop,cfg);
		}).delegate(".legentpicker-slider-handle-top,.legentpicker-slider-handle-bottom","mousedown",function(event){//滑动
			var left = parseFloat($(this).css("left"));
			var that = this;
			window.document.onmousemove = function(ev){
				ev.preventDefault();
				var offset = ev.pageX-event.pageX;
				var _left = left + Math.round(offset/step)*step;
				if(_left < -4){
					_left = -4;
				}else if(_left >= cfg.totalWidth - 4){
					_left = cfg.totalWidth - 4;
				}
				if(_left >= -4){
					$(that).css("left",_left+"px");
				}
				changeGisRange($drop,cfg);
			};
			window.document.onmouseup = function(){
				window.document.onmousemove = null;
				window.document.onmouseup = null;
				changeGisRange($drop,cfg);
			};
		});
	}
	//
	function changeGisRange($drop,cfg){
		var width = parseFloat($drop.find(".legentpicker-legend-block").css("width"));
		var left = Math.abs(parseFloat($drop.find(".legentpicker-slider-handle-top").css("left")));
		var bottom = Math.abs(parseFloat($drop.find(".legentpicker-slider-handle-bottom").css("left")));
		var topIndex = Math.round(left/width);
		var bottomIndex = Math.round(bottom/width);
		var min = null;
		var max = null;
		switch (cfg.seq) {
			case "decrease":// 递减
				if(topIndex > 0){
					if("index" == cfg.type){
						min = cfg.numData2[topIndex-1];
					}else{
						min = cfg.numData[topIndex-1];
					}
				}
				if(bottomIndex < cfg.numData.length+1){
					if("index" == cfg.type){
						max = cfg.numData2[bottomIndex-1];
					}else{
						max = cfg.numData[bottomIndex-1];
					}
				}
				if(min && max && min > max){
					var temp = min;
					min = max;
					max = temp;
				}
				break;
			case "increase":// 递增
				if(topIndex > 0){
					if("index" == cfg.type){
						max = cfg.numData2[cfg.numData.length-topIndex];
					}else{
						max = cfg.numData[cfg.numData.length-topIndex];
					}
				}
				if(bottomIndex < cfg.numData.length+1){
					if("index" == cfg.type){
						min = cfg.numData2[cfg.numData.length-bottomIndex];
					}else{
						min = cfg.numData[cfg.numData.length-bottomIndex];
					}
				}
				if(min && max && min > max){
					var temp = min;
					min = max;
					max = temp;
				}
				break;
			default:
				break;
		}
		if ($.isArray(cfg.layerId)) {
			cfg.layerId.forEach(function(id) {
				GIS.GridShow.changeRange(cfg.gisId,id,{min:min,max:max});
			})
		} else {
			GIS.GridShow.changeRange(cfg.gisId,cfg.layerId,{min:min,max:max});
		}
		
	}
	var CFLegentpicker = function(settings){
		var options = {
			appendTo: '',//控件所在容器
			h_legend : 27,//色例每个格子的高度
			totalWidth : 400,//色例总宽度
			seq : 'increase',// 默认垂直顺序为递增，递增为increase，递减为decrease
			gisId : null,//GIS容器
			layerId : null,//GIS图层Id
			productId : null,//产品ID
			height : null,//产品高度
			unit : null,//单位
			type : "value",//gis色斑过滤方式[index(根据数组下标)|value(根据数组值)]
			isShowSlider : true//是否显示滑块
		};
		this.config = $.extend({},options, settings);
		
		var legentconfig = CafeJS.Legentconfig().cfg();
		var proId = this.config.productId;
		if(legentconfig[this.config.productId+"_"+this.config.height]){
			proId = this.config.productId+"_"+this.config.height;
		}
		if (!legentconfig[proId]) return;
		this.config.numData = legentconfig[proId].levels;//色例的数值数组，为空则滑竿不显示
		this.config.numData2 = legentconfig[proId].compareValue;//色例的数值数组，为空则滑竿不显示
		this.config.imageData = legentconfig[proId].imageData;//色例的颜色数组
		this.$drop = getNodeHtml(this.config);
		_eves(this.$drop,this.config);
		if(this.config.appendTo){
			$(this.config.appendTo).append(this.$drop);
		}
	}
	
	CFLegentpicker.prototype = {
		clear : function(){
			this.$drop.remove();
		}
	};
	
	CafeJS.Legentpicker = function(settings){
		var drop =new CFLegentpicker(settings);
		return drop;
	}
});